home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / c / cp1.zip / DFREE1.C < prev    next >
C/C++ Source or Header  |  1993-04-30  |  5KB  |  107 lines

  1. ===========================================================================
  2.  BBS: The Abacus * HST/DS * Potterville MI
  3. Date: 04-28-93 (18:45)             Number: 165
  4. From: WAYNE MATSON                 Refer#: NONE
  5.   To: CHRIS DOWNS                   Recvd: NO  
  6. Subj: get disk free space   1/2      Conf: (36) C Language
  7. ---------------------------------------------------------------------------
  8. CD>How can I get the amount of free space on a hard disk?  I tried calling DOS
  9.   >with function 1B.  But that (or the code afterwards) sent the program into
  10.   >never never land.  Could someone please provide a helpful little snippet?
  11.   >Thanks,
  12.  
  13. Chris
  14.     Here is a small program I whip up for you to show how this can be
  15.     done.  The main code you want is GetDiskSpace(), but I have created
  16.     a complete working program to make sure it use properly debugged.
  17.  
  18. snip-------------------------->------------------------------------------------
  19. /******************************************************************************
  20.  ** DiskFree.C                                                               **
  21.  **     Display disk total, used and free space.                             **
  22.  **                                                                          **
  23.  ** Author: Wayne Matson                                                     **
  24.  **   Date: 04/27/93                                                         **
  25.  **                                                                          **
  26.  ******************************************************************************/
  27. #include <stdio.h>
  28. #include <dos.h>
  29.  
  30. typedef unsigned long   Ulong;
  31.  
  32. #define DosInt21        0x21
  33. #define GetFreeSpace    0x36
  34.  
  35. static char     *CommaDelimStr(char *str);
  36. static int      GetDiskSpace(int drv, Ulong *ttlDrv, Ulong *ttlFree);
  37.  
  38. int main(int argc, char **argv) {
  39. char            *usage = "\tUsage: DiskFree drv:";
  40. char            totalBytesStr[13];
  41. char            totalFreeStr[13];
  42. char            totalUsedStr[13];
  43. char            formatStr[10];
  44. int             drvNum = 0;
  45. Ulong           totalBytesNum;
  46. Ulong           totalFreeNum;
  47.  
  48.     if (argc > 2) {
  49.         puts(usage);
  50.         exit(1);
  51.     }
  52.  
  53.     if (argc == 2)
  54.         drvNum = toupper(*argv[1]) - 'A' + 1;
  55.  
  56.     if (GetDiskSpace(drvNum, &totalBytesNum, &totalFreeNum)) {
  57.         printf("\tInvalid Drive Letter %s", argv[1]);
  58.         exit(2);
  59.     }
  60.  
  61.     sprintf(totalBytesStr, "%lu", totalBytesNum);
  62.     sprintf(totalFreeStr, "%lu", totalFreeNum);
  63.     sprintf(totalUsedStr, "%lu", totalBytesNum-totalFreeNum);
  64.  
  65.     CommaDelimStr(totalBytesStr);
  66.     CommaDelimStr(totalFreeStr);
  67.     CommaDelimStr(totalUsedStr);
  68.  
  69.     sprintf(formatStr, "  %%%ds  %%s\n",strlen(totalBytesStr));
  70.  
  71.     printf(formatStr, totalBytesStr, "bytes total disk space");
  72.     printf(formatStr, totalUsedStr,  "bytes used");
  73.     printf(formatStr, totalFreeStr, "bytes free");
  74.  
  75.     return 0;
  76. }
  77.  
  78. static char *CommaDelimStr(char *str) {
  79. /******************************************************************************
  80.  ** Make a numeric string into a comma delimited numeric string.             **
  81.  **                                                                          **
  82.  ** Syntax:                                                                  **
  83.  ** char *CommaDelimStr(char *str)                                           **
  84.  **     str     = char pointer to numeric string which has enough space to   **
  85.  **               add the necessary commas. so if you pass '99999' then the  **
  86.  **               str pointer must be large enough the accomodate the comma  **
  87.  **               like '99,999'.                                             **
  88.  **                                                                          **
  89.  ** returns a pointer str if success or NULL if str is a zero length string. **
  90.  ******************************************************************************/
  91. char            *dynStr = NULL;
  92. char            *dynPtr;
  93. char            *strPtr;
  94. int             strLen;
  95. int             strCommas;
  96. int             i,x;
  97.  
  98.     strLen      = strlen(str);
  99.     if (NULL == str || 0 == strLen)
  100.     return NULL;
  101. >>> Continued to next message
  102. --- RAMail 3.3
  103.  * Origin: ABACUS BBS - Chaparral,NM. (505) 824-0049 HST DS (1:381/85)
  104. SEEN-BY: 1/211 11/2 4 13/13 101/1 108/89 109/25 110/69 114/5 123/19 124/1
  105. SEEN-BY: 153/752 154/40 77 157/2 159/100 125 575 950 203/23 209/209 280/1
  106. SEEN-BY: 390/1 396/1 5 15 2270/1 2440/5 3603/20
  107.